package io.geekidea.boot.test.controller;

import io.geekidea.boot.auth.annotation.Permission;
import io.geekidea.boot.common.enums.SysLogType;
import io.geekidea.boot.framework.annotation.Log;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.test.dto.CardDto;
import io.geekidea.boot.test.query.CardQuery;
import io.geekidea.boot.test.service.CardService;
import io.geekidea.boot.test.vo.CardVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 卡片 控制器
 *
 * @author geekidea
 * @since 2025-03-26
 */
@Slf4j
@RestController
@Tag(name = "卡片")
@RequestMapping("/admin/card")
public class CardController {

    @Autowired
    private CardService cardService;

    /**
     * 添加卡片
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.ADD)
    @Operation(summary = "添加卡片")
    @PostMapping("/addCard")
    @Permission("card:add")
    public ApiResult addCard(@Valid @RequestBody CardDto dto) {
        log.info("添加卡片：{}", dto);
        boolean flag = cardService.addCard(dto);
        return ApiResult.result(flag);
    }

    /**
     * 修改卡片
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.UPDATE)
    @Operation(summary = "修改卡片")
    @PostMapping("/updateCard")
    @Permission("card:update")
    public ApiResult updateCard(@Valid @RequestBody CardDto dto) {
        log.info("修改卡片：{}", dto);
        boolean flag = cardService.updateCard(dto);
        return ApiResult.result(flag);
    }

    /**
     * 删除卡片
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.DELETE)
    @Operation(summary = "删除卡片")
    @PostMapping("/deleteCard/{id}")
    @Permission("card:delete")
    public ApiResult deleteCard(@PathVariable Long id) {
        log.info("删除卡片：{}", id);
        boolean flag = cardService.deleteCard(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取卡片详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取卡片详情")
    @PostMapping("/getCard/{id}")
    @Permission("card:info")
    public ApiResult<CardVo> getCard(@PathVariable Long id) {
        log.info("获取卡片详情：{}", id);
        CardVo cardVo = cardService.getCardById(id);
        return ApiResult.success(cardVo);
    }

    /**
     * 获取卡片分页列表
     *
     * @param query
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取卡片分页列表")
    @PostMapping("/getCardPage")
    @Permission("card:page")
    public ApiResult<CardVo> getCardPage(@Valid @RequestBody CardQuery query) {
        log.info("获取卡片分页列表：{}", query);
        Paging<CardVo> paging = cardService.getCardPage(query);
        return ApiResult.success(paging);
    }

}
